import type { FlowExportObject } from "@vue-flow/core";

export default defineEventHandler(async (event): Promise<ReplyYesOrNo> => {
  const flowData = await readBody<ReqPayLoad<FlowExportObject | undefined>>(event);
  if (flowData.data) {
    const storage = useStorage("data");
    const user = await storage.getItem<JSONed<ReqPayLoad<UserType>>>("user");
    const hasStorage = user ? await storage.hasItem(`users:${user.json.data.nickname}`) : false;
    if (user && hasStorage)
      storage.setItem(`users:${user.json.data.nickname}`, { flow: flowData.data });
    else
      storage.setItem("flow", flowData.data);

    return { msg: "ok" };
  }
  return { msg: "no data" };
});
